package com.itheima.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: itcast_health
 * @create: 2019-10-28 21:50
 * @author: li he
 * @description:
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Map<String, Object> getDateMemberNumber(Date[] value1) throws Exception {
        List<String> monthslist = new ArrayList<>();
        List<Long> memberCountlist = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();

        Calendar startcalendar = Calendar.getInstance();
        Calendar endcalendar = Calendar.getInstance();
        startcalendar.setTime(value1[0]);
        endcalendar.setTime(value1[1]);
        boolean flag = true;
        while (flag){
            String starttime = DateUtils.parseDate2String(startcalendar.getTime());
            String endtime = DateUtils.parseDate2String(endcalendar.getTime());
            monthslist.add(starttime);
            long number = memberDao.getDateMemberNumber(starttime);
            if(starttime.equals(endtime)){
                flag = false;
            }
            memberCountlist.add(number);
            startcalendar.add(startcalendar.DAY_OF_MONTH,1);
        }
        /*for (;startDate[0] <= endDate[0]; startDate[0]++){
            for (;startDate[1] <= endDate[1]; startDate[1]++){
                for (;startDate[2] <= endDate[2]; startDate[2]++){
                    String date = startDate[0] + "-" + startDate[1] + "-" +startDate[2];
                    monthslist.add(date);
                    long number = memberDao.getDateMemberNumber(date);
                    memberCountlist.add(number);
                }
            }
        }*/

        map.put("months",monthslist);
        map.put("memberCount",memberCountlist);
        System.out.println("months = " + monthslist);
        System.out.println("memberCount = " + memberCountlist);
        System.out.println("map = " + map);
        return map;
    }

    @Override
    public Map<String, Object> getMemberReport() {

        Map<String, Object> map = new HashMap<>();

        List<String> monthList = new ArrayList<>();
        //获取前12个月的数组集合

        Calendar calendar = Calendar.getInstance();
        //向前调整12个月
        calendar.add(Calendar.MONTH, -12);

        for (int i = 0; i < 12; i++) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date time = calendar.getTime();
            String month = sdf.format(time);

            monthList.add(month);

            calendar.add(calendar.MONTH, 1);
        }

        System.out.println("monthList = " + monthList);

        ArrayList<Object> memberCountList = new ArrayList<>();

        for (String month : monthList) {
            month = month + "-31";

            long count = memberDao.findCountByMonth(month);

            memberCountList.add(count);
        }

        map.put("months", monthList);
        map.put("memberCount", memberCountList);
        return map;
    }



    @Override
    public Map<String, Object> getMemberScaleReport() {


        Map<String, Object> totalMap = new HashMap<>();

        //1.获取会员性别比例报表
        Map<String, Object> genderScaleMap = getGenderScaleMap();
        totalMap.put("genderScaleMap", genderScaleMap);

        //2.获取会员年龄比例报表
        Map<String, Object> ageScaleMap = getAgeScaleMap();
        totalMap.put("ageScaleMap",ageScaleMap);

        return totalMap;
    }
    /**
     * 1.会员性别比例
     * */
    private Map<String,Object> getGenderScaleMap(){

        //1.根据性别 0.未知 1.男性 2.女性
        Map<String, Object> genderScaleMap = new HashMap<>();
        List<String> genders = memberDao.findGenders();
        List<Object> genderMapList = new ArrayList<>();
        List<String> genderStrList = new ArrayList<>();
        for (String gender : genders) {
            switch (gender) {
                case "1": {
                    genderStrList.add("男性");
                    long count = memberDao.findCountByGender(gender);
                    Map<String, Object> genderMap = new HashMap<>();
                    genderMap.put("name", "男性");
                    genderMap.put("value", count);
                    genderMapList.add(genderMap);
                    break;
                }
                case "2": {
                    genderStrList.add("女性");
                    long count = memberDao.findCountByGender(gender);
                    Map<String, Object> genderMap = new HashMap<>();
                    genderMap.put("name", "女性");
                    genderMap.put("value", count);
                    genderMapList.add(genderMap);
                    break;
                }
                default: {
                    genderStrList.add("未知");
                    long count = memberDao.findCountByGender(gender);
                    Map<String, Object> genderMap = new HashMap<>();
                    genderMap.put("name", "未知");
                    genderMap.put("value", count);
                    genderMapList.add(genderMap);
                    break;
                }
            }
        }
        genderScaleMap.put("genders", genderStrList);
        genderScaleMap.put("genderCount", genderMapList);
        return genderScaleMap;
    }
    /**
     * 2.获取会员年龄比例报表
     *  按照会员的年龄段（可以指定几个年龄段，例如0-18、18-30、30-45、45以上）来展示各个年龄段的数量
     **/
    private Map<String,Object> getAgeScaleMap(){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        ArrayList<String> dataList = new ArrayList<>();

        //当前日期
        String zero = sdf.format(calendar.getTime());
        dataList.add(zero);

        //向前调整18年
        calendar.add(Calendar.YEAR,-18);
        //十八岁
        String eighteen = sdf.format(calendar.getTime());
        dataList.add(eighteen);
        //向前调整30年
        calendar.add(Calendar.YEAR,-12);
        //三十岁
        String thirty = sdf.format(calendar.getTime());
        dataList.add(thirty);
        //向前调整45年
        calendar.add(Calendar.YEAR,-15);
        //四十五
        String fortyFive = sdf.format(calendar.getTime());
        dataList.add(fortyFive);
        //向前调整145年
        calendar.add(Calendar.YEAR,-100);
        //四十五
        String hundred = sdf.format(calendar.getTime());
        dataList.add(hundred);


        List<String> ageList = new ArrayList<>();
        ageList.add("0-18岁");
        ageList.add("18-30岁");
        ageList.add("30-45岁");
        ageList.add("45岁以上");


        ArrayList<Object> countMapList = new ArrayList<>();

        for (int i = 0; i < ageList.size(); i++) {
            HashMap<Object, Object> countMap = new HashMap<>();
            /*获取name值*/
            String ageItem = ageList.get(i);
            countMap.put("name",ageItem);
            /*获取value值*/
            String startDate = dataList.get(i + 1);
            String endDate = dataList.get(i);
            long count = memberDao.findCountByData(startDate,endDate);
            countMap.put("value",count);
            countMapList.add(countMap);

        }

        Map<String, Object> ageScaleMap = new HashMap<>();

        ageScaleMap.put("ageList",ageList);
        ageScaleMap.put("countMapList",countMapList);

        return ageScaleMap;
    }

}